package com.suitsoft.ut.web.dabian;

import java.util.Date;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.annotations.Token;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Team;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 最终成绩评定查看
 *@author Suineg
 */
@Results({@Result(name = CrudActionSupport.RELOAD,location="end-score-view",type="redirect")})
public class EndScoreViewAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1335442195944163693L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private final String SCORE = "score";
	
	
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** 流程管理  服务层对象 */
	private ThesisFileManager fileManager;
	
	
	/** 题的分页 */
	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 题目 */
	private String titile;
	/** 教师名 */
	private String teacherName;
	
	@Override
	public String execute() throws Exception {
		loginTeacher = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(loginTeacher == null){
			addActionMessage("请使用教师账号");
			return SUCCESS;
		}
		boolean f = false;
		for(Team t : loginTeacher.getTeamList()){
			if(Constants.TEAM_TYPE_DABIAN.equals(Integer.valueOf(t.getType()))){
				f = true;
			}
		}
		if(!f){
			addActionMessage("您未在答辩团队中。不能进行此操作");
			return SUCCESS;
		}
		selectManager.findBySelectTeacher(page, titile, teacherName, Constants.THESIS_FLAG_DABIAN, Constants.THSIS_STAUTS_DABIAN_MAKE_FRACTION_TABLE,loginTeacher);
		return SUCCESS;
	}
	
	/**
	 * 初始化最终成绩表
	 * @return
	 */
	public String score(){
		if(id == null ||id == 0l){
			addActionMessage("非法操作");
			return RELOAD;
		}
		initList();
		thesis = thesisManager.get(id);
		if(thesis == null){
			addActionMessage("非法操作:未找到题目");
			return RELOAD;
		}
		thesisFile = thesis.getThesisFile();
		return SCORE;
	}
	
	@LogAnnotation(log = "论文成绩评定表查看 - 保存成绩 ")
	@Token
	public String save(){
		if(id == null ||id == 0l){
			addActionMessage("非法操作");
			return RELOAD;
		}
		String score = thesis.getEndScore();
		String name = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId()).getTeacherName();
		Date date = new Date();
		
		try {
			thesis = thesisManager.get(id);
			if(thesis == null){
				addActionMessage("非法操作:未找到题目");
				return RELOAD;
			}
			
			thesis.setEndScore(score);
			
			thesisFile = thesis.getThesisFile();
			thesisFile.setEndScoreDate(date);
			thesisFile.setEndScoreName(name);
		//	thesisFile.setThesisStatus(Constants.THSIS_STAUTS_DABIAN_MAKE_FRACTION_TABLE);
			fileManager.save(thesisFile);
			
			thesisManager.save(thesis);
			addActionMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
		}
		return RELOAD;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public Page<Thesis> getPage() {
		return page;
	}
	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public String getTitile() {
		return titile;
	}
	public void setTitile(String titile) {
		this.titile = titile;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	@Autowired
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}

}

